﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace DAL
{
    public class DALNguoiDung
    {
        //kiem tra dang nhap
        public static bool Checklogin(BLLNguoiDung user)
        {
            var user_account = from us in DALCommon.TMDTDB.NguoiDungs
                               where (us.IDNguoiDung.Equals(user.Id)) && (us.MatKhau.Equals(MD5_ComputeHexaHash(user.Matkhau)))
                               select us;
            if (user_account.Count() == 0)
            {
                return false;
            }
            else return true;
        }
        //ma hoa pass
        public static string MD5_ComputeHexaHash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = Encoding.Default.GetBytes(text);
            byte[] hash = md5.ComputeHash(data);
            string hexaHash = "";
            foreach (byte b in hash)
            {
                hexaHash += String.Format("{0:x2}", b);
            }
            return hexaHash;
        }
        //kiem tra quyen
        public static string Checklogin_quyen(BLLNguoiDung user)
        {
            string quyen = "";
            var user_account = from us in DALCommon.TMDTDB.NguoiDungs
                               where (us.IDNguoiDung.Equals(user.Id)) && (us.MatKhau.Equals(MD5_ComputeHexaHash(user.Matkhau)))
                               select us.PhanQuyen;
            quyen = user_account.FirstOrDefault();
            return quyen;
           
        }
        //kiem tra tai khoan
        public static string CheckTaiKhoan(BLLNguoiDung user)
        {
            string taikhoan = "";
            var user_account = from us in DALCommon.TMDTDB.NguoiDungs
                               where us.IDNguoiDung==user.Id
                               select us.TienMat;
            taikhoan = user_account.FirstOrDefault();
            return taikhoan;
        }
        //update tai khoan sau khi thanh toan mua hang
        public static void UpdateTaiKhoan(BLLNguoiDung user)
        {
            NguoiDung nd = DALCommon.TMDTDB.NguoiDungs.Single<NguoiDung>(d => d.IDNguoiDung == user.Id);
            nd.TienMat = user.Tienmat;
            DALCommon.TMDTDB.SubmitChanges();
        }
        //dang ki nguoi dung moi'
        public static string DangKi(BLLNguoiDung user)
        {
                TMDTDataContext tmdt = new TMDTDataContext();
                NguoiDung n = new NguoiDung();
                n.IDNguoiDung = user.Id;
                n.MatKhau =MD5_ComputeHexaHash(user.Matkhau);
                n.PhanQuyen = "User";
                n.HoTen = user.Hoten;
                n.Email = user.Email;
                n.SoDienThoai = user.Sodienthoai;
                n.TienMat = "1000000";
                n.UserXau = false;
                DALCommon.TMDTDB.NguoiDungs.InsertOnSubmit(n);
                DALCommon.TMDTDB.SubmitChanges();
                return n.IDNguoiDung;

        }
        //xóa user
         public static void Delete(BLLNguoiDung user)
         {
             NguoiDung nd = DALCommon.TMDTDB.NguoiDungs.Single<NguoiDung>(n => n.IDNguoiDung == user.Id);
             DALCommon.TMDTDB.NguoiDungs.DeleteOnSubmit(nd);
             DALCommon.TMDTDB.SubmitChanges();
         }
        //lay danh sach nguoi dung
        public static List<BLL.BLLNguoiDung> Getall()
    {
        var sps = (from sp in DALCommon.TMDTDB.NguoiDungs
                   select
             new BLLNguoiDung(sp.IDNguoiDung, sp.MatKhau, sp.PhanQuyen, sp.HoTen, sp.Email, sp.SoDienThoai,(bool)sp.UserXau, sp.TienMat));
        return sps.ToList<BLLNguoiDung>();
    }
        // nguoi dung theo id
        public static List<BLL.BLLNguoiDung> GetbyId(BLLNguoiDung user)
        {
            var sps = (from sp in DALCommon.TMDTDB.NguoiDungs
                       where sp.IDNguoiDung==user.Id
                       select
                 new BLLNguoiDung(sp.IDNguoiDung, sp.MatKhau, sp.PhanQuyen, sp.HoTen, sp.Email, sp.SoDienThoai, (bool)sp.UserXau, sp.TienMat));
            return sps.ToList<BLLNguoiDung>();
        }
        //sua user
        public static void Update(BLLNguoiDung user)
        {
            NguoiDung nd = DALCommon.TMDTDB.NguoiDungs.Single<NguoiDung>(d => d.IDNguoiDung == user.Id);
            //nd.MatKhau =MD5_ComputeHexaHash(user.Matkhau);
            nd.HoTen = user.Hoten;
            nd.PhanQuyen = user.Phanquyen;
            nd.Email = user.Email;
            nd.SoDienThoai = user.Sodienthoai;
            nd.UserXau = user.UserXau;
            nd.TienMat = user.Tienmat;
            DALCommon.TMDTDB.SubmitChanges();
        }

        //public static string ThanhToan(BLLNguoiDung user)
        //{
        //    NguoiDung nd = DALCommon.TMDTDB.NguoiDungs.Single<NguoiDung>(d => d.IDNguoiDung == user.Id);
        //    nd.TienMat = user.Tienmat;
        //    DALCommon.TMDTDB.SubmitChanges();
        //    return nd.TienMat;
        //}

    }
}
